<template>
  <div class="StatisticsCompany">
    <el-button type="primary" style="margin-bottom: 10px;" size="mini" @click="addCompany">{{
      form.area == 0 ? '添加安阳总公司' : '添加安徽分公司'
      }}</el-button>
    <el-table border :data="tableData" style="width: 100%" height="600">
      <el-table-column prop="user_id" label="公司ID" align="center"></el-table-column>
      <el-table-column prop="company_name" label="公司名称" align="center"></el-table-column>
      <el-table-column prop="add_time" label="添加时间" align="center"></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" @click="deleteCompany(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog class="addCompany" :title="form.area == 0 ? '添加安阳总公司' : '添加安徽分公司'" :append-to-body="true" :visible.sync="dialogVisible" width="30%">
      <!-- <CompanyData v-if="dialogVisible" :date="date" /> -->
      <el-form ref="form" label-width="80px">
        <el-form-item label="公司名称" >
          <el-select filterable v-model="addCompanyIndex" placeholder="请选择要添加的公司">
            <el-option v-for="(item,index) in companyList" :key="index" :label="item.company_name" :value="index"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureAddCompany">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  pushCompany,
  deletePushCompany,
  addPlatformCompany,
  getPlatformHapproveList
} from "@/api/set";
export default {
  name: "StatisticsCompany",
  props:{
    form:{
      type:Object,
      default:{}
    }
  },
  data() {
    return {
      tableData: [],
      companyList: [],
      dialogVisible: false,
      addCompanyIndex:0,
    };
  },
  mounted() {
    this.initial();
  },
  methods: {
    async initial() {
      console.log('this.form',this.form);
      
      let result = await pushCompany({
        area:this.form.area
      });
      if (result.code == 200) {
        this.tableData = result.data;
      }
    },
    async addCompany() {
      let result = await getPlatformHapproveList({
        area:this.form.area
      });
      if (result.code == 200) {
        this.companyList = result.data.filter(item => {
          return !this.tableData.some(item2 => {
            return item2.user_id == item.user_id;
          });
        });
        this.dialogVisible = true;
      }
    },
    async sureAddCompany(){
        let result = await addPlatformCompany({
          user_id: this.companyList[this.addCompanyIndex].user_id,
          company_name: this.companyList[this.addCompanyIndex].company_name,
          area:this.form.area
        });
        if (result.code == 200) {
          this.$message({
            type: "success",
            message: "添加成功!"
          });
          this.dialogVisible = false;
          this.initial();
        }
    },
    deleteCompany(row) {
      this.$confirm("确定要删除该公司吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          let result = await deletePushCompany({
            id: row.id
          });
          if (result.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.initial();
          }
        })
        .catch(() => {
          //   this.$message({
          //     type: "info",
          //     message: "已取消删除"
          //   });
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.StatisticsCompany {
  
}
</style>
